namespace Disco.Ontology.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class BaseLine : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.ChangeDetails",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Action = c.String(nullable: false),
                        EntityName = c.String(nullable: false),
                        EntityKey = c.Guid(nullable: false),
                        OldValues = c.String(nullable: false, storeType: "xml"),
                        NewValues = c.String(nullable: false, storeType: "xml"),
                        ChangesetId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Changesets", t => t.ChangesetId, cascadeDelete: true)
                .Index(t => t.ChangesetId);
            
            CreateTable(
                "dbo.Changesets",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        ModifiedById = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Originators", t => t.ModifiedById)
                .Index(t => t.ModifiedById);
            
            CreateTable(
                "dbo.Originators",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        AuthorId = c.Long(nullable: false),
                        OriginId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Users", t => t.AuthorId, cascadeDelete: true)
                .ForeignKey("dbo.Origins", t => t.OriginId, cascadeDelete: true)
                .Index(t => t.AuthorId)
                .Index(t => t.OriginId);
            
            CreateTable(
                "dbo.Users",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Alias = c.String(nullable: false),
                        Token = c.String(nullable: false),
                        OriginId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Origins", t => t.OriginId)
                .Index(t => t.OriginId);
            
            CreateTable(
                "dbo.GroupMemberships",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        MembershipTypeId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                        Group_Id = c.Long(nullable: false),
                        User_Id = c.Long(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Groups", t => t.Group_Id)
                .ForeignKey("dbo.GroupMembershipTypes", t => t.MembershipTypeId)
                .ForeignKey("dbo.Users", t => t.User_Id)
                .Index(t => t.Group_Id)
                .Index(t => t.MembershipTypeId)
                .Index(t => t.User_Id);
            
            CreateTable(
                "dbo.Groups",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Alias = c.String(nullable: false),
                        DescriptionId = c.Long(nullable: false),
                        ParentId = c.Long(),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Descriptors", t => t.DescriptionId)
                .ForeignKey("dbo.Groups", t => t.ParentId)
                .Index(t => t.DescriptionId)
                .Index(t => t.ParentId);
            
            CreateTable(
                "dbo.Descriptors",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Name = c.String(nullable: false),
                        Description = c.String(),
                        CultureId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Cultures", t => t.CultureId)
                .Index(t => t.CultureId);
            
            CreateTable(
                "dbo.Cultures",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Code = c.String(nullable: false),
                        Name = c.String(),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.GroupMembershipTypes",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        DescriptionId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Descriptors", t => t.DescriptionId)
                .Index(t => t.DescriptionId);
            
            CreateTable(
                "dbo.Origins",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Uri = c.String(nullable: false),
                        DescriptionId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Descriptors", t => t.DescriptionId)
                .Index(t => t.DescriptionId);
            
            CreateTable(
                "dbo.Contents",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Title = c.String(),
                        Text = c.String(nullable: false),
                        CultureId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                        ChangeDetail_Id = c.Long(nullable: false),
                        ModifiedBy_Id = c.Long(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.ChangeDetails", t => t.ChangeDetail_Id)
                .ForeignKey("dbo.Cultures", t => t.CultureId)
                .ForeignKey("dbo.Originators", t => t.ModifiedBy_Id)
                .Index(t => t.ChangeDetail_Id)
                .Index(t => t.CultureId)
                .Index(t => t.ModifiedBy_Id);
            
            CreateTable(
                "dbo.NamedValues",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Name = c.String(nullable: false),
                        Value = c.String(nullable: false),
                        NamedValueSetId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.NamedValueSets", t => t.NamedValueSetId, cascadeDelete: true)
                .Index(t => t.NamedValueSetId);
            
            CreateTable(
                "dbo.NamedValueSets",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        OriginId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Origins", t => t.OriginId)
                .Index(t => t.OriginId);
            
            CreateTable(
                "dbo.PostReferences",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        ReferrerId = c.Long(nullable: false),
                        ReferreeId = c.Long(nullable: false),
                        ReferenceTypeId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.PostReferenceTypes", t => t.ReferenceTypeId)
                .ForeignKey("dbo.Posts", t => t.ReferreeId)
                .ForeignKey("dbo.Posts", t => t.ReferrerId)
                .Index(t => t.ReferenceTypeId)
                .Index(t => t.ReferreeId)
                .Index(t => t.ReferrerId);
            
            CreateTable(
                "dbo.PostReferenceTypes",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        DescriptionId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Descriptors", t => t.DescriptionId)
                .Index(t => t.DescriptionId);
            
            CreateTable(
                "dbo.Posts",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        PostTypeId = c.Long(nullable: false),
                        ContentId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                        ChangeDetail_Id = c.Long(nullable: false),
                        ModifiedBy_Id = c.Long(nullable: false),
                        Region_Id = c.Long(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.ChangeDetails", t => t.ChangeDetail_Id)
                .ForeignKey("dbo.Contents", t => t.ContentId)
                .ForeignKey("dbo.Originators", t => t.ModifiedBy_Id)
                .ForeignKey("dbo.PostTypes", t => t.PostTypeId)
                .ForeignKey("dbo.Regions", t => t.Region_Id)
                .Index(t => t.ChangeDetail_Id)
                .Index(t => t.ContentId)
                .Index(t => t.ModifiedBy_Id)
                .Index(t => t.PostTypeId)
                .Index(t => t.Region_Id);
            
            CreateTable(
                "dbo.PostTypes",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        DescriptionId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Descriptors", t => t.DescriptionId)
                .Index(t => t.DescriptionId);
            
            CreateTable(
                "dbo.Ratings",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Score = c.Int(nullable: false),
                        PostId = c.Long(nullable: false),
                        UserId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                        ChangeDetail_Id = c.Long(nullable: false),
                        ModifiedBy_Id = c.Long(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.ChangeDetails", t => t.ChangeDetail_Id)
                .ForeignKey("dbo.Originators", t => t.ModifiedBy_Id)
                .ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
                .ForeignKey("dbo.Posts", t => t.PostId, cascadeDelete: true)
                .Index(t => t.ChangeDetail_Id)
                .Index(t => t.ModifiedBy_Id)
                .Index(t => t.UserId)
                .Index(t => t.PostId);
            
            CreateTable(
                "dbo.Tags",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        DescriptionId = c.Long(nullable: false),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Descriptors", t => t.DescriptionId)
                .Index(t => t.DescriptionId);
            
            CreateTable(
                "dbo.Regions",
                c => new
                    {
                        Id = c.Long(nullable: false, identity: true),
                        Code = c.String(nullable: false),
                        DescriptionId = c.Long(nullable: false),
                        ParentId = c.Long(),
                        Key = c.Guid(nullable: false),
                        Modified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Descriptors", t => t.DescriptionId)
                .ForeignKey("dbo.Regions", t => t.ParentId)
                .Index(t => t.DescriptionId)
                .Index(t => t.ParentId);
            
            CreateTable(
                "dbo.RelatedTags",
                c => new
                    {
                        Tag_Id = c.Long(nullable: false),
                        RelatedTag_Id = c.Long(nullable: false),
                    })
                .PrimaryKey(t => new { t.Tag_Id, t.RelatedTag_Id })
                .ForeignKey("dbo.Tags", t => t.Tag_Id)
                .ForeignKey("dbo.Tags", t => t.RelatedTag_Id)
                .Index(t => t.Tag_Id)
                .Index(t => t.RelatedTag_Id);
            
            CreateTable(
                "dbo.PostTags",
                c => new
                    {
                        Post_Id = c.Long(nullable: false),
                        Tag_Id = c.Long(nullable: false),
                    })
                .PrimaryKey(t => new { t.Post_Id, t.Tag_Id })
                .ForeignKey("dbo.Posts", t => t.Post_Id)
                .ForeignKey("dbo.Tags", t => t.Tag_Id)
                .Index(t => t.Post_Id)
                .Index(t => t.Tag_Id);
            
        }
        
        public override void Down()
        {
            DropForeignKey("dbo.Posts", "Region_Id", "dbo.Regions");
            DropForeignKey("dbo.Regions", "ParentId", "dbo.Regions");
            DropForeignKey("dbo.Regions", "DescriptionId", "dbo.Descriptors");
            DropForeignKey("dbo.PostReferences", "ReferrerId", "dbo.Posts");
            DropForeignKey("dbo.PostReferences", "ReferreeId", "dbo.Posts");
            DropForeignKey("dbo.PostTags", "Tag_Id", "dbo.Tags");
            DropForeignKey("dbo.PostTags", "Post_Id", "dbo.Posts");
            DropForeignKey("dbo.RelatedTags", "RelatedTag_Id", "dbo.Tags");
            DropForeignKey("dbo.RelatedTags", "Tag_Id", "dbo.Tags");
            DropForeignKey("dbo.Tags", "DescriptionId", "dbo.Descriptors");
            DropForeignKey("dbo.Ratings", "PostId", "dbo.Posts");
            DropForeignKey("dbo.Ratings", "UserId", "dbo.Users");
            DropForeignKey("dbo.Ratings", "ModifiedBy_Id", "dbo.Originators");
            DropForeignKey("dbo.Ratings", "ChangeDetail_Id", "dbo.ChangeDetails");
            DropForeignKey("dbo.Posts", "PostTypeId", "dbo.PostTypes");
            DropForeignKey("dbo.PostTypes", "DescriptionId", "dbo.Descriptors");
            DropForeignKey("dbo.Posts", "ModifiedBy_Id", "dbo.Originators");
            DropForeignKey("dbo.Posts", "ContentId", "dbo.Contents");
            DropForeignKey("dbo.Posts", "ChangeDetail_Id", "dbo.ChangeDetails");
            DropForeignKey("dbo.PostReferences", "ReferenceTypeId", "dbo.PostReferenceTypes");
            DropForeignKey("dbo.PostReferenceTypes", "DescriptionId", "dbo.Descriptors");
            DropForeignKey("dbo.NamedValues", "NamedValueSetId", "dbo.NamedValueSets");
            DropForeignKey("dbo.NamedValueSets", "OriginId", "dbo.Origins");
            DropForeignKey("dbo.Contents", "ModifiedBy_Id", "dbo.Originators");
            DropForeignKey("dbo.Contents", "CultureId", "dbo.Cultures");
            DropForeignKey("dbo.Contents", "ChangeDetail_Id", "dbo.ChangeDetails");
            DropForeignKey("dbo.ChangeDetails", "ChangesetId", "dbo.Changesets");
            DropForeignKey("dbo.Changesets", "ModifiedById", "dbo.Originators");
            DropForeignKey("dbo.Originators", "OriginId", "dbo.Origins");
            DropForeignKey("dbo.Originators", "AuthorId", "dbo.Users");
            DropForeignKey("dbo.Users", "OriginId", "dbo.Origins");
            DropForeignKey("dbo.Origins", "DescriptionId", "dbo.Descriptors");
            DropForeignKey("dbo.GroupMemberships", "User_Id", "dbo.Users");
            DropForeignKey("dbo.GroupMemberships", "MembershipTypeId", "dbo.GroupMembershipTypes");
            DropForeignKey("dbo.GroupMembershipTypes", "DescriptionId", "dbo.Descriptors");
            DropForeignKey("dbo.GroupMemberships", "Group_Id", "dbo.Groups");
            DropForeignKey("dbo.Groups", "ParentId", "dbo.Groups");
            DropForeignKey("dbo.Groups", "DescriptionId", "dbo.Descriptors");
            DropForeignKey("dbo.Descriptors", "CultureId", "dbo.Cultures");
            DropIndex("dbo.Posts", new[] { "Region_Id" });
            DropIndex("dbo.Regions", new[] { "ParentId" });
            DropIndex("dbo.Regions", new[] { "DescriptionId" });
            DropIndex("dbo.PostReferences", new[] { "ReferrerId" });
            DropIndex("dbo.PostReferences", new[] { "ReferreeId" });
            DropIndex("dbo.PostTags", new[] { "Tag_Id" });
            DropIndex("dbo.PostTags", new[] { "Post_Id" });
            DropIndex("dbo.RelatedTags", new[] { "RelatedTag_Id" });
            DropIndex("dbo.RelatedTags", new[] { "Tag_Id" });
            DropIndex("dbo.Tags", new[] { "DescriptionId" });
            DropIndex("dbo.Ratings", new[] { "PostId" });
            DropIndex("dbo.Ratings", new[] { "UserId" });
            DropIndex("dbo.Ratings", new[] { "ModifiedBy_Id" });
            DropIndex("dbo.Ratings", new[] { "ChangeDetail_Id" });
            DropIndex("dbo.Posts", new[] { "PostTypeId" });
            DropIndex("dbo.PostTypes", new[] { "DescriptionId" });
            DropIndex("dbo.Posts", new[] { "ModifiedBy_Id" });
            DropIndex("dbo.Posts", new[] { "ContentId" });
            DropIndex("dbo.Posts", new[] { "ChangeDetail_Id" });
            DropIndex("dbo.PostReferences", new[] { "ReferenceTypeId" });
            DropIndex("dbo.PostReferenceTypes", new[] { "DescriptionId" });
            DropIndex("dbo.NamedValues", new[] { "NamedValueSetId" });
            DropIndex("dbo.NamedValueSets", new[] { "OriginId" });
            DropIndex("dbo.Contents", new[] { "ModifiedBy_Id" });
            DropIndex("dbo.Contents", new[] { "CultureId" });
            DropIndex("dbo.Contents", new[] { "ChangeDetail_Id" });
            DropIndex("dbo.ChangeDetails", new[] { "ChangesetId" });
            DropIndex("dbo.Changesets", new[] { "ModifiedById" });
            DropIndex("dbo.Originators", new[] { "OriginId" });
            DropIndex("dbo.Originators", new[] { "AuthorId" });
            DropIndex("dbo.Users", new[] { "OriginId" });
            DropIndex("dbo.Origins", new[] { "DescriptionId" });
            DropIndex("dbo.GroupMemberships", new[] { "User_Id" });
            DropIndex("dbo.GroupMemberships", new[] { "MembershipTypeId" });
            DropIndex("dbo.GroupMembershipTypes", new[] { "DescriptionId" });
            DropIndex("dbo.GroupMemberships", new[] { "Group_Id" });
            DropIndex("dbo.Groups", new[] { "ParentId" });
            DropIndex("dbo.Groups", new[] { "DescriptionId" });
            DropIndex("dbo.Descriptors", new[] { "CultureId" });
            DropTable("dbo.PostTags");
            DropTable("dbo.RelatedTags");
            DropTable("dbo.Regions");
            DropTable("dbo.Tags");
            DropTable("dbo.Ratings");
            DropTable("dbo.PostTypes");
            DropTable("dbo.Posts");
            DropTable("dbo.PostReferenceTypes");
            DropTable("dbo.PostReferences");
            DropTable("dbo.NamedValueSets");
            DropTable("dbo.NamedValues");
            DropTable("dbo.Contents");
            DropTable("dbo.Origins");
            DropTable("dbo.GroupMembershipTypes");
            DropTable("dbo.Cultures");
            DropTable("dbo.Descriptors");
            DropTable("dbo.Groups");
            DropTable("dbo.GroupMemberships");
            DropTable("dbo.Users");
            DropTable("dbo.Originators");
            DropTable("dbo.Changesets");
            DropTable("dbo.ChangeDetails");
        }
    }
}
